using UnityEngine;

public class Settings
{
    #region About Grid Generate
    private static int m_RemainNumberLSL;
    private static int m_RemainNumberUSL;
    private static DigMethod m_SetDigMethod;

    public static DigMethod SetDigMethod
    {
        get => m_SetDigMethod;
        set
        {
            m_SetDigMethod = value;
            PlayerPrefs.SetInt("SettedDigMethod", (int)value);
        }
    }
    // range [22, 80/DigNumberUSL]
    public static int RemainNumberLSL
    {
        get => m_RemainNumberLSL;
        set
        {
            if (value > m_RemainNumberUSL)
                m_RemainNumberLSL = m_RemainNumberUSL;
            else if (value < 22)
                m_RemainNumberLSL = 22;
            else if (value > 80)
                m_RemainNumberLSL = 80;
            else
                m_RemainNumberLSL = value;

            PlayerPrefs.SetInt("RemainNumberLSL", RemainNumberLSL);
        }
    }
    // range [22/DigNumberUSL, 80]
    public static int RemainNumberUSL
    {
        get => m_RemainNumberUSL;
        set
        {
            if (value < m_RemainNumberLSL)
                m_RemainNumberUSL = m_RemainNumberLSL;
            else if (value < 22)
                m_RemainNumberUSL = 22;
            else if (value > 80)
                m_RemainNumberUSL = 80;
            else
                m_RemainNumberUSL = value;

            PlayerPrefs.SetInt("RemainNumberUSL", RemainNumberUSL);
        }
    }

    private static readonly int DEFAULT_SET_DIG_METHOD = 1;
    private static readonly int DEFAULT_REMAIN_NUMBER_LSL = 22;
    private static readonly int DEFAULT_REMAIN_NUMBER_USL = 30;

    private static readonly string KEY_SET_DIG_METHOD = "SettedDigMethod";
    private static readonly string KEY_REMAIN_NUMBER_LSL = "RemainNumberLSL";
    private static readonly string KEY_REMAIN_NUMBER_USL = "RemainNumberUSL";
    #endregion

    public struct SettingItems
    {
        public DigMethod settedDigMethod;
        public int remainNumberLSL;
        public int remainNumberUSL;
    }

    public static void ReadSettings()
    {
        SetDigMethod = (DigMethod)PlayerPrefs.GetInt(KEY_SET_DIG_METHOD);
        RemainNumberLSL = PlayerPrefs.GetInt(KEY_REMAIN_NUMBER_LSL);
        RemainNumberUSL = PlayerPrefs.GetInt(KEY_REMAIN_NUMBER_USL);
    }

    public static void ResetSettings()
    {
        PlayerPrefs.SetInt(KEY_SET_DIG_METHOD, DEFAULT_SET_DIG_METHOD);
        PlayerPrefs.SetInt(KEY_REMAIN_NUMBER_LSL, DEFAULT_REMAIN_NUMBER_LSL);
        PlayerPrefs.SetInt(KEY_REMAIN_NUMBER_USL, DEFAULT_REMAIN_NUMBER_USL);

        ReadSettings();
    }

    public static SettingItems GetSettings()
    {
        ReadSettings();

        SettingItems settings = new SettingItems();
        settings.settedDigMethod = SetDigMethod;
        settings.remainNumberLSL = RemainNumberLSL;
        settings.remainNumberUSL = RemainNumberUSL;

        return settings;
    }
}
